package com.itboye.shop.me;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.shop.R;
import com.itboye.shop.base.BaseActivity;
import com.itboye.shop.common.MyEditText;
import com.itboye.shop.net.ResultEntity;
import com.itboye.shop.presenter.UserPresenter;
import com.itboye.shop.util.helper.utils.TimeCount;
import com.itboye.shop.util.loadutil.MAlert;

import java.util.Observable;
import java.util.Observer;

/**
 * 注册
 */
public class RegisterActivity extends BaseActivity implements Observer {
    TextView tvTitle;
    ImageView back;
    TextView login_pwd,register_yzm,txt_send_yzm;
    MyEditText refister_phone,refister_yzm,refister_pass;
    UserPresenter userPresenter;
    Button refister_ok,refister_login;

    private TimeCount time;// 倒计时

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_register);
        tvTitle.setText("注册");
        userPresenter=new UserPresenter(this);

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.login_pwd:
                String phone=refister_phone.getText().toString();
                if (phone.equals("")) {
                    MAlert.alert("请输入手机号码", Gravity.CENTER);
                    return;
                }
                getYzm(phone);

                time = new TimeCount(60000, 1000, login_pwd);// 构造CountDownTimer对象
                time.start();
                break;
            case R.id.refister_login:

                break;
            case R.id.refister_ok:
                String phone01=refister_phone.getText().toString();
                String yzm=refister_yzm.getText().toString();
                String pass=refister_pass.getText().toString();
                if(phone01.equals("")){
                    MAlert.alert("请输入手机号");
                    return;
                }
                if(yzm.equals("")){
                    MAlert.alert("请输入验证码");
                    return;
                }
                if(pass.equals("")){
                    MAlert.alert("请输入密码");
                    return;
                }
                userPresenter.YanZhengverificationCode("+86",phone01,yzm,"1");

                break;
            case R.id.back:
                finish();
                break;
        }
    }
    private void getYzm(String phone) {
        userPresenter.sendVerificationCode("+86", phone, 1 + "");
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity resultEntity=handlerError(data);
        if (resultEntity!=null) {
            if (resultEntity.getEventType()== UserPresenter.send_code_success) {//发送验证码
                MAlert.alert(resultEntity.getData(),Gravity.CENTER);
            }
            if(resultEntity.getEventType()== UserPresenter.send_code_success) {
                MAlert.alert(resultEntity.getData(),Gravity.CENTER);
            }
            if (resultEntity.getEventType()== UserPresenter.yanzheng_code_success) {//验证验证码
                MAlert.alert(resultEntity.getData(),Gravity.CENTER);
                showProgressDialog("请稍后...", true);
                userPresenter.register(refister_phone.getText().toString(),refister_pass.getText().toString(),"+86",refister_yzm.getText().toString(),0+"");
            }
            if(resultEntity.getEventType()== UserPresenter.yanzheng_code_success) {
                MAlert.alert(resultEntity.getData(),Gravity.CENTER);
            }
            if (resultEntity.getEventType()== UserPresenter.register_success) {//注册
                MAlert.alert(resultEntity.getData(),Gravity.CENTER);
                finish();
            }
            if(resultEntity.getEventType()== UserPresenter.register_success) {
                MAlert.alert(resultEntity.getData(),Gravity.CENTER);
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}

